package com.isoftstone.merchant.service.payment.impl;

import com.isoftstone.common.Page;
import com.isoftstone.merchant.dao.feeUser.MFeeUserMapper;
import com.isoftstone.merchant.dao.mdept.MDeptMapper;
import com.isoftstone.merchant.dao.payment.PaymentMapper;
import com.isoftstone.merchant.entity.mdept.MDept;
import com.isoftstone.merchant.entity.payment.Payment;
import com.isoftstone.merchant.service.payment.PaymentService;
import com.isoftstone.util.PageData;
import com.isoftstone.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: zhaojg
 * @Description
 * @Date: Created in 17:08 2017/12/9
 * @Modified by:
 */

@Service
public class PaymentServiceImpl implements PaymentService{

    @Autowired
    PaymentMapper paymentMapper;
    //线下收费begin
    @Autowired
    MDeptMapper mDeptMapper;
    @Autowired
    MFeeUserMapper mFeeUserMapper;

    public static String  deptstr;
    //线下收费end

    @Override
    public List<Payment> delayPaymentlist(String openAccount,String feeId) throws Exception {
        return paymentMapper.delayPaymentlist(openAccount,feeId);
    }
    @Override
    public List<Payment> queryAlreadyPaymentOpenAccount(String openAccount,String feeId,String beginDate,String endDate) throws Exception {
        return paymentMapper.queryAlreadyPaymentOpenAccount(openAccount,feeId,beginDate,endDate);
    }

    @Override
    public List<Payment> getmFeeUserlistPage(Page page) throws Exception {

       if(null != page.getPd().get("orgIds") && !page.getPd().get("orgIds").equals("")){
           page.getPd().put("orgIds", Arrays.asList(page.getPd().get("orgIds")));
       }
        return paymentMapper.getmFeeUserlistPage(page);
    }

    //收费查询删除
   public void deleteFormPayment(Long payId){
       paymentMapper.deleteByPrimaryKey(payId);
    }

    //催缴查询
    public Payment getPaymentById(Long payId,Long projectId){
       return paymentMapper.selectPaymentById(payId,projectId);
    }

    //修改催缴次数
    public void editUrgeCnt(Payment payment){
        paymentMapper.updatePaymentUrgeCut(payment);
    }

    /*
    * 收费导出查询
    * */
    public List<Payment> getPaymentListByExport(Payment payments){
        return paymentMapper.selectPaymentListByExport(payments);
    }

    @Override
    public void addPaymentToBatch(Payment payment){
        paymentMapper.insertPaymentToBatch(payment);
    }

    @Override
    public List<Payment> queryProjectDetail(String corpId, String projectId, String feeId) throws Exception {
        return paymentMapper.queryProjectDetail(corpId,projectId,feeId);
    }

    @Override
    public List<Payment> queryProjectDetailByStatus(String corpId, String projectId, String feeId, String status) throws Exception {
        return paymentMapper.queryProjectDetailByStatus(corpId,projectId,feeId,status);
    }

    @Override
    public List<Payment> queryMemberShipDetail(String corpId, String projectId, String feeId) throws Exception {
        return paymentMapper.queryMemberShipDetail(corpId,projectId,feeId);
    }

    @Override
    public List<Payment> queryMemberShipDetailByStatus(String corpId, String projectId, String feeId, String status) throws Exception {
        return paymentMapper.queryMemberShipDetailByStatus(corpId,projectId,feeId,status);
    }

    @Override
    public Payment queryPaymentByPayIdAndFactAmt(Long payId, String factAmt) throws Exception {
        return paymentMapper.queryPaymentByPayIdAndFactAmt(payId,factAmt);
    }

    @Override
    public void updatePaymentByPayId(Payment payment) throws Exception {
        paymentMapper.selectByPrimaryKey(payment.getPayId());
    }

    @Override
    public void updatePayment(Long payId) throws Exception {
        paymentMapper.updatePayment(payId);
    }

    @Override
    public void updatePaymentForIsSelect(Long payId) throws Exception {
        paymentMapper.updatePaymentForIsSelect(payId);
    }

    @Override
    //线下收费用begin
    public List<Payment> getPaymentlistPage(Page page) throws Exception {
        return paymentMapper.selectPaymentlistPage(page);
    }
    @Override
    public List<Payment> getAllPayMentlistPage(List<Payment> paymentList)  {
        try {
            for(Payment payment:paymentList){
                String deptId=mFeeUserMapper.selectDeptId(payment.getFeeUserId());
                if(payment.getFactAmt()==null){
                    payment.setFactAmt(0.00);
                }
                getdeptStr(deptId);
                if(StringUtils.isNotBlank(deptstr)){
                    payment.setFeeUserDept(deptstr);
                    deptstr="";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return paymentList;
    }

    @Override
    @Transactional
    public void underCharge(Payment payment) throws Exception {
        paymentMapper.updateByPrimaryKeySelective(payment);
    }

    @Override
    public void addPayment(Payment payment) throws Exception {
        paymentMapper.addPayment(payment);
    }

    @Override
    public Payment getPayUserNumber(Page page) throws Exception {
        return paymentMapper.getPayUserNumber(page);
    }


    private String getdeptStr(String deptId){
        try {
            MDept dept=mDeptMapper.getdeptById(deptId);
            if(dept!=null) {
                if ("0".equals(dept.getSuperDeptId())) {
                    deptstr += dept.getDeptName() + "  ";
                    return deptstr;
                } else {
                    deptstr += dept.getDeptName() + "  ";
                    return getdeptStr(dept.getSuperDeptId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//线下收费用end


    @Override
    public String countPayment(PageData params) {
        return this.paymentMapper.countPayment(params);
    }

    @Override
    public String sumPayment(PageData params) {
        return this.paymentMapper.sumPayment(params);
    }

    @Override
    public Integer getAllPayNumber(Page page) throws Exception {
        return paymentMapper.getAllPayNumber(page);
    }

    @Override
    public Integer getTodayPayNumber(Page page) throws Exception {
        return paymentMapper.getTodayPayNumber(page);
    }

    @Override
    public String countBindUser(PageData params) {
        return paymentMapper.countBindUser(params);
    }

    @Override
    public Double getAllPayMoney(Page page) throws Exception {
        return paymentMapper.getAllPayMoney(page);
    }

    @Override
    public Double getTodayPayMoney(Page page) throws Exception {
        return paymentMapper.getTodayPayMoney(page);
    }

    @Override
    public Payment getPaymentByPayId(Long payId) throws Exception {
        return paymentMapper.selectByPrimaryKey(payId);
    }

    @Override
    public Payment queryPaymentByPayIdAndFeeUserId(Long payId, String feeUserId) throws Exception {
        return paymentMapper.queryPaymentByPayIdAndFeeUserId(payId,feeUserId);
    }

    @Override
    public void updatePaymentStatus(Payment payment) throws Exception {
        paymentMapper.updateByPrimaryKeySelective(payment);
    }

    @Override
    public Double getTodayPayMoneys(PageData todayPageDate) throws Exception {
        return paymentMapper.getTodayPayMoneys(todayPageDate);
    }

    @Override
    public Double getAllMoney(PageData allPageDate) throws Exception {
        return paymentMapper.getAllMoney(allPageDate);
    }

    @Override
    public Integer getTodayPayNumbers(PageData todayNumberPageDate) throws Exception {
        return paymentMapper.getTodayPayNumbers(todayNumberPageDate);
    }

    @Override
    public Integer getAllPayNumbers(PageData allNumberPageDate) throws Exception {
        return paymentMapper.getAllPayNumbers(allNumberPageDate);
    }

}
